﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Reports_Inventory_StockInformation_StockLedger : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable dtGridDetails = new DataTable();
    string lid = string.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
            txtFromDate.Text = "01/04/" + Session["finyear"].ToString();
            txtToDate.Text = "31/03/" + (Convert.ToInt32(Session["finyear"].ToString()) + 1);
        }
    }

    protected void BindGrid()
    {
        DataTable dt = DU.getdatausingprocedure("USP_IVR_GetItemDetails");
        DataView dv = dt.DefaultView;
        dv.RowFilter = "StockTypeId =1";
        grdStockLedger.DataSource = dv.ToTable() ;
        grdStockLedger.DataBind();
    }

    protected void grdStockLedger_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdStockLedger.PageIndex = e.NewPageIndex;
        ChkSelectAll.Checked = false;
        BindGrid();
    }

    protected void BindGridDetails(string from,string to)
    {
            DataTable dtLname = new DataTable();
        if (from != string.Empty && to != string.Empty)
        {
            dtLname.Columns.Add("Itemid", typeof(string));
            dtLname.Columns.Add("LName", typeof(string));

            lid = string.Empty;

            int rowcount = grdStockLedger.Rows.Count;
            for (int i = 0; i < rowcount; i++)
            {
                CheckBox chkopt = (CheckBox)grdStockLedger.Rows[i].FindControl("chkOpt");
                HiddenField hidITMID = (HiddenField)grdStockLedger.Rows[i].FindControl("hidITMID");
                Label lblITMDESC=(Label)grdStockLedger.Rows[i].FindControl("lblITMDESC");
                if (chkopt.Checked == true)
                {
                    lid += hidITMID.Value + ",";
                    DataRow dr = dtLname.NewRow();
                    dr["Itemid"] = hidITMID.Value.ToString();
                    dr["LName"] = lblITMDESC.Text;
                    dtLname.Rows.Add(dr);
                }
            }
        }
        dtlstStockDetail.DataSource = dtLname;
        dtlstStockDetail.DataBind();
    }

    protected void dtlstStockDetail_ItemBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            GridView grd = (GridView)e.Item.FindControl("grdDetailsfinal");
            ImageButton imgShow = (ImageButton)e.Item.FindControl("imgShow");
            HiddenField hidItemID = (HiddenField)e.Item.FindControl("hidItemID");
            
            SqlParameter[] sqlpara = {
                                         new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtFromDate.Text))),
                                         new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtToDate.Text))),
                                         new SqlParameter("@ITMID",lid)
                                        };
            dtGridDetails = DU.getdatausingprocedure("USP_IVR_GetItemSummaryInPrimaryUnit", sqlpara);

            DataView dvFinal= dtGridDetails.DefaultView;
            string expr = "ITMID=" + hidItemID.Value.ToString();
            dvFinal.RowFilter = expr;
         
            grd.DataSource = dvFinal.ToTable();
            grd.DataBind();
            imgShow.Attributes.Add("onclick", "return expandDetailsInGrid('" + imgShow.ClientID + "','" + grd.ClientID + "');");
        }
    }

    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        BindGridDetails(txtFromDate.Text, txtToDate.Text);
        pnlOption.Visible = false;
        pnlGrid.Visible = true;
    }

    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGridDetails(txtFromDate.Text, txtToDate.Text);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
}
